#ifndef __STREAMHANDLER_H__
#define __STREAMHANDLER_H__

#include <IHandler.h>
#include <IFormatter.h>
#include <LoggingGlobal.h>
#include <LogRecord.h>

namespace logging
{
    // Forward Declarations
    struct LogRecord;

    template <typename StreamT>
	class StreamHandler : public IHandler
	{
	public:	    
	    StreamHandler( StreamT & stream ) : IHandler(), 
		m_logLevel( Finest ),
		m_stream( stream )
		{
		}

	    void close() {}
	    void flush() { m_stream.flush(); }
	    
	    LogLevel logLevel() const { return( m_logLevel ); }
	    void setLogLevel( LogLevel logLevel ) { m_logLevel = logLevel; }

	    void processRecord( const LogRecord & record )
		{
		    if( m_formatter == 0 )
			return;

		    if( record.m_logLevel < m_logLevel )
			return;

		    m_stream << m_formatter->format( record ) << std::endl;
		}

	    bool ready() { return( m_formatter != 0 ); }

	protected:
	    LogLevel 	m_logLevel;
	    StreamT 	& m_stream;
	};
}

#endif // __STREAMHANDLER_H__
